package com.qiang.eduservice.controller;


import com.qiang.commonutils.R;
import com.qiang.eduservice.client.VodClient;
import com.qiang.eduservice.entity.EduVideo;
import com.qiang.eduservice.service.EduVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }
    //删除小节
    //TODO 删除小节的同时 要把视频给删除掉。 需完善：
    @DeleteMapping("/{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小节id查询视频id 调用方法实现删除视频
        EduVideo video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        //判断小节当中是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据id 远程调用方法实现删除视频
            vodClient.removeAlyVideo(videoSourceId);
        }
        videoService.removeById(id);
        return R.ok();
    }

    //修改小节 TODO
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }



    //根据id查询小节
    @GetMapping("/getVideoById/{videoId}")
    public R getVideoById(@PathVariable String videoId){
        EduVideo video = videoService.getById(videoId);
        System.out.println(video);
        return R.ok().data("video",video);
    }
}

